package Dao.Role.Imp;

import Dao.Role.AdminDao;
import Entity.Role.Admin;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImp implements AdminDao {

    private PreparedStatement ps = null;
    private ResultSet rs = null;


    @Override
    public List<Admin> getAdminByName(String name, Connection conn) {
        List<Admin> admins = new ArrayList<>();
        try {
            String sql = "select admin_id,admin_name,admin_password,admin_realname,collegeid,admin_email,email_code " +
                    "from admin where admin_name = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            while (rs.next()) {
                String adminId = rs.getString("admin_id");
                String adminName = rs.getString("admin_name");
                String adminPassword = rs.getString("admin_password");
                String realName = rs.getString("admin_realname");
                String collegeId = rs.getString("collegeid");
                String adminEmail = rs.getString("admin_email");
                String emailCode = rs.getString("email_code");
                Admin admin = new Admin(adminId, adminName, adminPassword, realName, collegeId, adminEmail, emailCode);
                admins.add(admin);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return admins;
    }

    @Override
    public Integer updatePassByNO(String NO, String newPass, Connection conn) {
        int res = 0;
        try {
            String sql = "update admin set admin_password = ? where admin_name = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, newPass);
            ps.setString(2, NO);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }

    @Override
    public Integer updateEmailByNO(String NO, String newEmail, Connection conn) {
        int res = 0;
        try {
            String sql = "update admin set email = ? where admin_name = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, newEmail);
            ps.setString(2, NO);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }

    @Override
    public Integer updateEmailCodeByNO(String NO, String emailCode, Connection conn) {
        int res = 0;
        try {
            String sql = "update admin set email_code = ? where admin_name = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, emailCode);
            ps.setString(2, NO);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }
}
